package com.hzwq.demo2.permission.controller;

import com.hzwq.demo2.permission.entity.Permission;
import com.hzwq.demo2.permission.service.IPermissionService;
import com.hzwq.demo2.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhaochunlin
 * @since 2020-08-13
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    IPermissionService permissionService;

    @RequestMapping("/list")
    public ModelAndView toList() {
        ModelAndView mav = new ModelAndView("permission/list");
        return mav;
    }

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> query() {
        List<Permission> list = permissionService.list();
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("msg", "查询成功");
        result.put("code", "0");
        result.put("data", list);
        return result;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ModelAndView toEdit(Permission permission) {
        ModelAndView mav = new ModelAndView("permission/edit");
        if (permission != null && permission.getId() != null) {
            permission = permissionService.getById(permission.getId());
        }
        mav.addObject("permission", permission);
        return mav;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Result<Permission> save(Permission permission) {
        permissionService.saveOrUpdate(permission);
        return new Result<Permission>("0", "保存成功");
    }

    @RequestMapping("/del")
    @ResponseBody
    public Result<Permission> del(Integer id){
        //拿到整个权限表的数据
        List<Permission> permissionList = permissionService.list();
        //保存所有节点的id
        List<Integer> list=new ArrayList<>();
        if(id!=null){
            //进行遍历查询出所有节点
            list = selectAll(id, permissionList, list);
            System.out.println(list);
            if (list!=null){
                list.add(id);
                permissionService.del(list);
            }else{
                permissionService.removeById(id);
            }
        }
        return new Result<Permission>("0","删除成功！");
    }

    private List selectAll(Integer id, List<Permission> permissionList, List list){

        //暂存匹配后的id
        List<Integer> ts =new ArrayList<>();

        //查询看这个id下面是否有子节点
        for (Permission test:permissionList) {
            //遍历这个权限列表,并看是否与父id匹配，如果匹配了，记录下id,并放到列表中
            if (test.getPid().equals(id)){
                ts.add(test.getId());//234   56  7
            }
        }
        if (ts.size()==0){
            return null;
        }else{
            list.addAll(ts);//将所有ts保存到list中去  list:234 56  7
            //说明还存在子节点,接着查询子节点的子节点
            for (int i = 0; i <ts.size() ; i++) {
                selectAll(ts.get(i),permissionList,list);
            }
        }
        return list;
    }

}
